{include file="header/index" /}
		<div class="container whitebg">
			<div class="topnav clearfix">
				<div class="title fl">
					<h3>新增行政区域</h3>
				</div>
			</div>
			<form class="form-horizontal">
				<div class="form-group">
					<input type="hidden" name="a_id" id="a_id" value="{if condition='(empty($data))'}{else /}{$data.a_id}{/if}">
					<label for="company_name" class="col-sm-2 control-label">行政区域名称</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" id="company_name" value="{if condition='(empty($data))'}{else /}{$data.company_name}{/if}">
					</div>
				</div>
				<div class="form-group">
				<label class="col-sm-2 control-label">所属运营中心</label>
				<div class="col-sm-4">
					<select id="operation" name="" class="form-control">
						<option value="">请选择运营中心</option>
						{if condition="(empty($operation))"}
						{else /}
						{volist name="operation" id="op"}
						<option value="{$op.o_id}" {if condition='((isset($data)) AND ($op.o_id == $data.operation))'}selected{else /}{/if}>{$op.company_name}</option>
						{/volist}
						{/if}
					</select>
				</div>
			</div>
				<div class="form-group add_center">
					<label class="col-sm-2 control-label">负责区域</label>
					<div class="col-sm-8 choosecity">
						<label class="fl control-label mr10">省</label>
						<select name="" id="province"  onChange="moveCity()">
							<option value="">选择省份</option>
							{volist name="Province" id="pr"}
							<option value="{$pr.id}" {if condition='((isset($data)) AND ($pr.id == $data.province_id))'}selected{else /}{/if}>{$pr.area_name}</option>
							{/volist}
						</select>
						<label class="fl control-label mr10">市</label>
						<select name="" id="city"	onChange="moveArea()">
							<option value="">选择市</option>

							{volist name="City" id="ci"}
							{if condition='((isset($data)) AND ($ci.id == $data.city_id))'}
							<option value="{$ci.id}" {if condition='((isset($data)) AND ($ci.id == $data.city_id))'}selected{else /}{/if}>{$ci.area_name}</option>
							{/if}
							{/volist}

						</select>
						<label class="fl control-label mr10">区/县</label>						
						<select id="Area" name="administrative" class="form-control administrative">
							<option value="">选择区/县</option>
							{volist name="Area" id="Ar"}
							{if condition='((isset($data)) AND ($Ar.id == $data.area_id))'}
							<option value="{$Ar.id}" {if condition='((isset($data)) AND ($Ar.id == $data.area_id))'}selected{else /}{/if}>{$Ar.area_name}</option>
							{/if}
							{/volist}
						</select>
						<div class="btn btn-default" onclick="addselect('',this)"><i class="glyphicon glyphicon-plus"></i></div>
					</div>
				</div>


				<div class="form-group">
					<label for="address" class="col-sm-2 control-label">详细地址</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" id="address" value="{if condition='(empty($data))'}{else /}{$data.address}{/if}">
					</div>
				</div>
				<div class="form-group">
					<label for="name" class="col-sm-2 control-label">联系人</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" id="name" value="{if condition='(empty($data))'}{else /}{$data.name}{/if}">
					</div>
				</div>
				<div class="form-group">
					<label for="phone" class="col-sm-2 control-label">联系电话</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" id="phone" value="{if condition='(empty($data))'}{else /}{$data.phone}{/if}">
					</div>
				</div>
				{if condition='((!empty($data)))'}
				<div class="form-group" style="width: 65%; text-align: center;">
					<label class="radio-inline"><input type="radio" name="engineers_status" id="inlineRadio1" value="1" {if condition='((!empty($data)) AND ($data.status == 1))'}checked{else /}checked{/if}>正常</label>
					<label class="radio-inline"><input type="radio" name="engineers_status" id="inlineRadio2" value="0" {if condition='((!empty($data)) AND ($data.status == 0))'}checked{/if}>冻结</label>
				</div>
				{/if}
				<div class="form-group">
					<label for="Remarks" class="col-sm-2 control-label">备注</label>
					<div class="col-sm-5">
						<textarea class="form-control" id="Remarks" rows="3" placeholder="请输入备注信息" onkeydown="checkMaxInput(this,100)" onkeyup="checkMaxInput(this,100)" onfocus="checkMaxInput(this,100)" onblur="checkMaxInput(this,100);resetMaxmsg()">{if condition='(empty($data))'}{else /}{$data.note}{/if}</textarea>
					</div>
				</div>
			</form>
			<form class="form-inline btnlists">
				<button type="button" id="but" class="btn btn-primary">添加保存</button>
				<button class="btn btn-default" onclick="backurl(this)">返回</button>
			</form>
		</div>
	</body>

</html>
<script type="text/javascript">

	//提交
	$("#but").click(function () {

	    //获取行政中心ID
		var province_id = $('#province').val();
		var province_name = $('#province option:selected').text();
		var city_id = $('#city').val();
        var city_name = $('#city option:selected').text();
        var administrative_id = [];
	    var administrative_name = [];
        var administrative = $(".administrative");
        $.each(administrative,function(key,val){
            administrative_id.push($(val).find("option:selected").val());
            administrative_name.push($(val).find("option:selected").text());
		});
		var company_name = $('#company_name').val();
		var operation = $('#operation').val();
		var address = $('#address').val();
		var name = $('#name').val();
		var phone = $('#phone').val();
		var note = $('#Remarks').val();
		var a_id = $('#a_id').val();
        var status = $('input:radio:checked').val();
        $.get("{:url('Operationmanagement/doEditProfile')}",{status:status,a_id:a_id,company_name:company_name,operation:operation,address:address,name:name,phone:phone,note:note,province_id:province_id,province_name:province_name,city_id:city_id,city_name:city_name,area_id:administrative_id,area_name:administrative_name},function (flag) {
            console.log(flag);
            if (flag.code == 0){
                layer.confirm(flag.msg, {
                    btn: ['确认'] //按钮
                });
            }else {
                layer.confirm(flag.msg, {
                    btn: ['确认'] //按钮
                }, function(){
                    history.back();
                });
            }
        });

    });

	//负责多区域
    addArea();
	function addArea() {
        {if condition='((isset($data.area)))'}
			{foreach $data.area as $k => $vo}
        		{if condition='($k != 0)'}
        			addselect({$vo.area_id});
        		{/if}
			{/foreach}
        {/if}
    }

    //选择地址
    function moveCity() {
        var province = $("#province").val();
        $.get("{:url('Operationmanagement/selectCity')}",{province:province},function (data) {
//            console.log(data);
            $("#city").empty();
            $("#city").append($("<option>").val("").text("请选择市"));
            for (var i = 0;i<data.length;i++){
                var option = $("<option>").val(data[i].id).text(data[i].area_name);
                $("#city").append(option);
            }

        })
    }

    //选择地址
    function moveArea() {
        var city = $("#city").val();

        $.get("{:url('Operationmanagement/selectArea')}",{city:city},function (data) {
            $("#Area").empty();
            $("#Area").append($("<option>").val("").text("请选择区/县"));
            for (var i = 0;i<data.length;i++){
                var option = $("<option>").val(data[i].id).text(data[i].area_name);
                $("#Area").append(option);
            }

        })
    }

	//返回上一页
	function backurl() {
		history.back();
	}

	//多行文本输入框剩余字数计算  
	function checkMaxInput(obj, maxLen) {
		if(obj == null || obj == undefined || obj == "") {
			return;
		}
		if(maxLen == null || maxLen == undefined || maxLen == "") {
			maxLen = 100;
		}

		var strResult;
		var $obj = $(obj);
		var newid = $obj.attr("id") + 'msg';

		if(obj.value.length > maxLen) { //如果输入的字数超过了限制  
			obj.value = obj.value.substring(0, maxLen); //就去掉多余的字  
			strResult = '<span id="' + newid + '" class=\'Max_msg\' ><br/>剩(' + (maxLen - obj.value.length) + ')字</span>'; //计算并显示剩余字数  
		} else {
			strResult = '<span id="' + newid + '" class=\'Max_msg\' ><br/>剩(' + (maxLen - obj.value.length) + ')字</span>'; //计算并显示剩余字数  
		}

		var $msg = $("#" + newid);
		if($msg.length == 0) {
			$obj.after(strResult);
		} else {
			$msg.html(strResult);
		}
	}

	//清空剩除字数提醒信息  
	function resetMaxmsg() {
		$("span.Max_msg").remove();
	}
	
	//增加 删除
	function addselect(area,obj) {
        var city = $("#city").val();
        var administrative_id = [];
        $("select[name='administrative']").each(function(){
            administrative_id.push($(this).val());
        });
        $.get("{:url('Operationmanagement/selectArea')}",{city:city},function (data) {
                var html = '<div class="form-group">';
                html += '<label class="col-sm-2 control-label"></label>';
                html += '<div class="col-sm-8 choosecity">';
                html += '<label class="fl control-label mr10" style="visibility: hidden">省</label>';
                html += '<select style="visibility: hidden">';
                html += '</select>';
                html += '<label class="fl control-label mr10" style="visibility: hidden">市</label>';
                html += '<select style="visibility: hidden">';
                html += '</select>';
                html += '<label class="fl control-label mr10">区/县</label>';
                html += '<select id="Area" name="administrative" class="form-control administrative">';
                html += '<option value="">请选择区/县</option>';
                for (var i = 0; i < data.length; i++) {
                    //去掉已经选择的区域
                    if ($.inArray(data[i].id.toString(),administrative_id) == -1) {
                    	html += '<option value="' + data[i].id + '"';
                        if (data[i].id == area){
                            html += 'selected>' + data[i].area_name + '</option>';
						}else {
                            html += '>' + data[i].area_name + '</option>';
						}
                    }
				}
                html += '</select>';
                html += '<div class="btn btn-default" onclick="delselect(this)"><i class="glyphicon glyphicon-minus"></i></div>';
                html += '</div>';
                html += '</div>';
            /**
			 *判断是否选择城市为空
             */
              if($("#city").val()!=""){
                  $(".add_center").after(html);
			  }else{
                  layer.msg("请先选择城市",{
                  time:2000
              });
              }
        })
	}
	
	function delselect(obj){
		$(obj).parent().remove();
	}
</script>
